#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

#include <QStringListModel>
#include <QStandardItemModel>

#include <QWidgetAction>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

//    qDebug() << "页面索引总数: " << ui->stackedWidget->count();
//    qDebug() << "获取当前页面的索引: " << ui->stackedWidget->currentIndex();
//    qDebug() << "获取当前页面: " << ui->stackedWidget->currentWidget();
    ui->stackedWidget->setCurrentIndex(0);

    // QListView的使用 本文链接：https://blog.csdn.net/sinat_31608641/article/details/126088067
    // QStringListModel的使用
    QStringList strlist;
    strlist.append("A");
    strlist.append("B");
    strlist.append("C");

    QStringListModel *Model = new QStringListModel(strlist);
    //ui->listView->setStyleSheet("QListView::item { padding: 5px; }"); //内边距属性(Padding)
    ui->listView->setStyleSheet("QListView::item { margin-bottom: 15px; } QListView { font: 75 12pt 'Arial'; }"); //外边距属性(Margin)
    //ui->listView->setStyleSheet("QListView { font: 75 18pt 'Arial'; }");

    ui->listView->setModel(Model); // listview设置Model
    // listView绑定事件
    connect(ui->listView,SIGNAL(clicked(QModelIndex)),this,SLOT(slot_listView_event(QModelIndex)));

    // treeWidget绑定事件
    connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(slot_treeWidget_event(QTreeWidgetItem*,int)));


    // 创建 QStandardItemModel
    QStandardItemModel *model = new QStandardItemModel;

    // 添加带有图标的项
    QList<QStandardItem *> items;
    items.append(new QStandardItem("Item 1"));
    items.first()->setIcon(QIcon(":/ico/apkpush1.png")); // 设置图标
    model->appendRow(items);

    items.clear(); // 清除已使用的项列表，为下一个项做准备
    items.append(new QStandardItem("Item 2"));
    items.first()->setIcon(QIcon(":/ico/ToolPc1.png")); // 设置图标
    model->appendRow(items);

    items.clear(); // 清除已使用的项列表，为下一个项做准备
    items.append(new QStandardItem("Item 3"));
    items.first()->setIcon(QIcon(":/ico/ToolVR1.png")); // 设置图标
    model->appendRow(items);

    // 将模型设置到 QListView
    ui->listView_2->setIconSize(QSize(32,32));
    //ui->listView_2->setGridSize(QSize(32,32));
    ui->listView_2->setModel(model);
    // listView绑定事件
    connect(ui->listView_2,SIGNAL(clicked(QModelIndex)),this,SLOT(slot_listView_event(QModelIndex)));


    connect(ui->listWidget_1,SIGNAL(itemClicked(QListWidgetItem *)),this,SLOT(slot_listWidget_event(QListWidgetItem *)));

    //ui->listWidget->setResizeMode(QListView::Adjust);
    ui->listWidget->setIconSize(QSize(64, 64)); // 设置图标大小

    // 加载图片并创建 QListWidgetItem
    QIcon icon(":/ico/apkpush1.png"); // 替换为你的图片路径
    QListWidgetItem *item = new QListWidgetItem;
    item->setIcon(icon);
    item->setText("D"); // 可选：设置文本

    // 将项添加到 QListWidget
    ui->listWidget->addItem(item);
    connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem *)),this,SLOT(slot_listWidget_event(QListWidgetItem *)));


    //本文链接：https://blog.csdn.net/weixin_52919504/article/details/133172808

//    ui->toolButton->setPopupMode(QToolButton::DelayedPopup);
//    ui->toolButton->setPopupMode(QToolButton::MenuButtonPopup);
    ui->toolButton->setPopupMode(QToolButton::InstantPopup);
    // 创建 QMenu
    QMenu *menu = new QMenu(ui->toolButton);

    // 添加 QAction 到 QMenu
    QAction *action1 = menu->addAction("选项 1");
    QAction *action2 = menu->addAction("选项 2");
    QAction *action3 = menu->addAction("选项 3");

    // 连接 QAction 的 triggered 信号到槽函数
    connect(action1, SIGNAL(triggered(bool)), this, SLOT(onActionTriggered(bool)));
    connect(action2, &QAction::triggered, this, &MainWindow::onActionTriggered);
    connect(action3, &QAction::triggered, this, &MainWindow::onActionTriggered);

    // 将 QMenu 设置为 QToolButton 的下拉菜单
    ui->toolButton->setMenu(menu);

    #if 0
    /* 可以使用 QSlider:: 来进行不同方向的刻度设置 */
    ui->horizontalSlider->setTickPosition(QSlider::TicksAbove);

    //设置范围值
    ui->horizontalSlider->setMinimum(0);
    ui->horizontalSlider->setMaximum(100);

    //设置值
    ui->horizontalSlider->setValue(50);
    QString strText = QString::number(ui->horizontalSlider->value());

    //刻度间隔
    ui->horizontalSlider->setTickInterval(10);

    //设置步长
    ui->horizontalSlider->setSingleStep(10);
    #endif
}

void MainWindow::onActionTriggered(bool checked)
{
    QAction *action = qobject_cast<QAction *>(sender());

    if (action)
    {
        qDebug() << "Selected:" << action->text() << "checked" << checked;
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slot_listView_event(QModelIndex index)
{
    QString str = index.data().toString();
    int row = index.row();
    qDebug() << "row: " << row << "str: " << str;

    ui->stackedWidget->setCurrentIndex(row);
}

void MainWindow::slot_listWidget_event(QListWidgetItem *item)
{
    qDebug() << "Clicked item:" << item->text();
    QString str = item->text();

    int row = ui->listWidget->currentRow(); // 获取当前选中项的行索引
    qDebug() << "row: " << row << "str: " << str;
    ui->stackedWidget->setCurrentIndex(row);
}

//原文链接：https://blog.csdn.net/qq_37529913/article/details/128312187
void MainWindow::switchPage()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());

    if(button==ui->pushButton)
        ui->stackedWidget->setCurrentIndex(0);
    else if(button==ui->pushButton_2)
        ui->stackedWidget->setCurrentIndex(1);
    else if(button==ui->pushButton_3)
        ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_pushButton_clicked()
{
    switchPage();
}

void MainWindow::on_pushButton_2_clicked()
{
    switchPage();
}

void MainWindow::on_pushButton_3_clicked()
{
    switchPage();
}

void MainWindow::on_pushButton_4_clicked()
{
    switchPage();
}

void MainWindow::slot_treeWidget_event(QTreeWidgetItem* item,int column)
{
    qDebug() << "item->text(column): " << item->text(column);
}


void MainWindow::on_pushButton_flod_vertical_clicked()
{
    if(ui->groupBox->isHidden())
    {
        ui->pushButton_flod_vertical->setIcon(QIcon(":/ico/fold_left.png"));
        ui->groupBox->setHidden(false);
    }
    else
    {
        ui->pushButton_flod_vertical->setIcon(QIcon(":/ico/fold_right.png"));
        ui->groupBox->setHidden(true);
    }
}

void MainWindow::on_pushButton_flod_horizontal_clicked()
{
    if(ui->groupBox_3->isHidden())
    {
        ui->pushButton_flod_horizontal->setIcon(QIcon(":/ico/fold_down.png"));
        ui->groupBox_3->setHidden(false);
    }
    else
    {
        ui->pushButton_flod_horizontal->setIcon(QIcon(":/ico/fold_up.png"));
        ui->groupBox_3->setHidden(true);
    }
}

void MainWindow::on_buttonBox_accepted()
{
    qDebug() << "on_buttonBox_accepted ";
}

void MainWindow::on_buttonBox_rejected()
{
    qDebug() << "on_buttonBox_rejected ";
}

void MainWindow::on_horizontalSlider_valueChanged(int value)
{
     qDebug() << "Slider_value" << value;
}

void MainWindow::on_dial_valueChanged(int value)
{
     qDebug() << "dial_value" << value;
}
